<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <!-- <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="报告ID" prop="reportId">
              <el-input v-model="queryParams.reportId" placeholder="请输入报告ID" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="领域" prop="field">
              <el-input v-model="queryParams.field" placeholder="请输入领域" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card> -->
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <!-- <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['system:dissent:export']">导出</el-button> -->
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" :search="false" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="dissentList">
        <el-table-column label="报告编号" align="center" prop="reportNo" width="200" />
        <el-table-column label="主体名称" align="center" prop="nickName" min-width="150" show-overflow-tooltip />
        <el-table-column label="统一社会信用代码/身份证号" align="center" prop="authId" min-width="190" />
        <el-table-column label="领域" align="center" prop="field" min-width="200" show-overflow-tooltip />
        <el-table-column label="时间范围" align="center" prop="beginTime" width="190">
          <template #default="{ row }">
            {{ row.beginTime ? row.beginTime.slice(0, 11) : '' }}- {{ row.endTime ? row.endTime.slice(0, 11) : '' }}
          </template>
        </el-table-column>
        <el-table-column label="下载次数" align="center" prop="downloadCount" width="90" />
        <el-table-column label="生成状态" align="center" prop="status" width="100">
          <template #default="{ row }">
            <el-tag :type="row.status == '成功' ? 'success' : row.status == '生成中' ? 'primary' : 'info'">{{ row.status }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="申请时间" align="center" prop="createTime" width="160" />
        <el-table-column label="生成时间" align="center" prop="reportTime" width="160" />
        <el-table-column label="操作" align="center" width="130">
          <template #default="{ row }">
            <el-tooltip v-if="row.status == '失败'" content="查看失败原因" placement="top">
              <el-button link type="primary" icon="Warning" @click="showReason(row)"></el-button>
            </el-tooltip>
            <template v-if="row.status == '成功'">
              <el-tooltip content="预览" placement="top">
                <el-button link type="primary" icon="View" @click="handlePreview(row)"></el-button>
              </el-tooltip>
              <el-tooltip content="下载" placement="top">
                <el-button link type="primary" icon="Download" @click="handleDown(row)" v-hasPermi="['system:oss:download']"></el-button>
              </el-tooltip>
              <el-tooltip content="查看详情" placement="top">
                <el-button link type="primary" icon="Document" @click="showDetail(row)"></el-button>
              </el-tooltip>
            </template>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 详情弹窗 -->
    <detailDialog ref="detailDialogRef"></detailDialog>
  </div>
</template>

<script setup name="Dissent" lang="ts">
import { getQueryList, previewReport } from '@/api/xydz/history/index';
import { DissentVO } from '@/api/xydz/dissent/types';
import detailDialog from './detail-dialog.vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const dissentList = ref<DissentVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const detailDialogRef = ref<InstanceType<typeof detailDialog>>();

const state = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    createDep: undefined,
    reportId: undefined,
    field: undefined,
    evidence: undefined,
    newReport: undefined,
    status: undefined,
    params: {}
  }
});

const { queryParams } = toRefs(state);

/** 查询异议信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await getQueryList(queryParams.value);
  dissentList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

onMounted(() => {
  getList();
});

//预览报告
const handlePreview = (row) => {
  proxy?.$preview.report(row.id);
};

//下载报告
const handleDown = (row) => {
  proxy?.$download.oss(row.ossId);
};

//查看失败原因
const showReason = (row) => {
  ElMessageBox.alert(row.reason ? row.reason : '无', '失败原因', { type: 'error' });
};

//查看详情
const showDetail = (row) => {
  detailDialogRef.value.show(row);
};
</script>
